package com.kobe.project.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.kobe.common.utils.DateUtils;
import com.kobe.project.utils.MailUtil;
import com.kobe.task.domain.SysOaTask;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.kobe.project.mapper.SysOaProjectMapper;
import com.kobe.project.domain.SysOaProject;
import com.kobe.project.service.ISysOaProjectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-18
 */
@Service
@RequiredArgsConstructor
public class SysOaProjectServiceImpl implements ISysOaProjectService
{
    @Autowired
    private SysOaProjectMapper sysOaProjectMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    private final MailUtil mailUtil;



    /**
     * 查询项目管理
     *
     * @param projectId 项目管理主键
     * @return 项目管理
     */
    @Override
    public SysOaProject selectSysOaProjectByProjectId(Long projectId)
    {
        return sysOaProjectMapper.selectSysOaProjectByProjectId(projectId);
    }

    /**
     * 查询项目管理列表
     *
     * @param sysOaProject 项目管理
     * @return 项目管理
     */
    @Override
    public List<SysOaProject> selectSysOaProjectList(SysOaProject sysOaProject)
    {
        //先查redis缓存
        List<SysOaProject> projectList = (List<SysOaProject>) redisTemplate.opsForValue().get("projectList");
        if (projectList != null && !projectList.isEmpty()) {
            //缓存存在TODO:
            //TODO: 缓存过期问题
            redisTemplate.opsForValue().set("projectList", projectList, 1, TimeUnit.HOURS);
            return projectList;
        }
        else {
            //缓存不存在
            projectList = sysOaProjectMapper.selectSysOaProjectList(sysOaProject);
            //查询成功，存入缓存
            redisTemplate.opsForValue().set("projectList", projectList, 1, TimeUnit.HOURS);

        }

        return sysOaProjectMapper.selectSysOaProjectList(sysOaProject);
    }

    /**
     * 新增项目管理
     *
     * @param sysOaProject 项目管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysOaProject(SysOaProject sysOaProject)
    {
        synchronized (SysOaProjectServiceImpl.class){
        sysOaProject.setCreateTime(DateUtils.getNowDate());
        int i = sysOaProjectMapper.insertSysOaProject(sysOaProject);
        mailUtil.sendMail("1797089786@qq.com", "有新项目"+sysOaProject.getProjectName());
        redisTemplate.opsForValue().set("projectList", null, 1, TimeUnit.HOURS);
        return i;
        }
    }

    /**
     * 修改项目管理
     *
     * @param sysOaProject 项目管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysOaProject(SysOaProject sysOaProject)
    {
        synchronized (SysOaProjectServiceImpl.class){
            sysOaProject.setUpdateTime(DateUtils.getNowDate());
            int i = sysOaProjectMapper.updateSysOaProject(sysOaProject);
            redisTemplate.opsForValue().set("projectList", null, 1, TimeUnit.HOURS);
            return i;
        }

    }

    /**
     * 批量删除项目管理
     *
     * @param projectIds 需要删除的项目管理主键
     * @return 结果
     */
    @Override
    public int deleteSysOaProjectByProjectIds(Long[] projectIds)
    {
        return sysOaProjectMapper.deleteSysOaProjectByProjectIds(projectIds);
    }

    /**
     * 删除项目管理信息
     *
     * @param projectId 项目管理主键
     * @return 结果
     */
    @Override
    public int deleteSysOaProjectByProjectId(Long projectId)
    {
        return sysOaProjectMapper.deleteSysOaProjectByProjectId(projectId);
    }

    @Override
    public List<SysOaTask> listByProjectId(Long projectId) {
        return sysOaProjectMapper.listByProjectId(projectId);
    }
}
